//*************************************************************************************
//* 作    者： 
//* 创建时间： 2021-10-09 09:37:28
//*  描    述：

//* ************************************************************************************
using System;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

namespace UFrame.Tween
{
    [System.Serializable]
    public class TweenLockMove : TweenBase<float>
    {
        public override bool Valid => m_body && m_target;
        public Transform m_body;
        public Transform m_target;
        protected Vector3 m_startPos;
        protected Vector3 m_localPos;

        protected override void OnUpdate(float factor, bool isFinished)
        {
            value = from + factor * (to - from);
            if(m_target)
            {
                var targetPos = m_target.TransformPoint(m_localPos);
                var pos = Vector3.Lerp(m_startPos, targetPos, value);
                m_body.transform.position = pos;
            }
        }

        public static TweenLockMove Create(Transform trans,Transform target,Vector3 worldPos,float duration = 1f, float delay = 0f)
        {
            TweenLockMove comp = Create<TweenLockMove>();
            comp.m_body = trans;
            comp.value = 0;
            comp.from = 0;
            comp.to = 1;
            comp.duration = duration;
            comp.delay = delay;
            comp.m_localPos = target.InverseTransformPoint(worldPos);
            comp.m_startPos = trans.position;
            comp.m_target = target;
            if (duration <= 0)
            {
                comp.Sample(1, true);
                comp.enabled = false;
            }
            return comp;
        }
    }
}
